import socket

# 创建 socket 对象
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 绑定地址和端口
# ip 和 port(端口号), windows中localhost代表本机服务
server_address = ('localhost', 12345)
server_socket.bind(server_address)


print(server_address)
# 监听连接
server_socket.listen()

print('等待客户端连接...')
connection, client_address = server_socket.accept()
print('客户端已连接:', client_address)

###
# 接收和发送数据
while True:
    data = connection.recv(1024)
    if not data:
        break
    print('收到客户端消息:', data.decode())
    connection.sendall('收到你的消息了'.encode())

# 关闭连接
connection.close()
server_socket.close()
